//
//  YZDailyWorkVC.m
//  VET
//
//  Created by Lam BG on 2018/7/10.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZDailyWorkVC.h"
#import "YZDailyWorkListVC.h"

// view
#import "YZTopScrollView.h"

// model
#import "YZDailyWorkItem.h"

// other
#import "YZRouter.h"
#import "YZAutoFormHeader.h"

// 第三方
#import "TBCityIconFont.h"

// category
#import "UIView+Frame.h"
#import "YZRouter+AutoVC.h"
#import "UIScrollView+EdgeBack.h"
#import "UIViewController+Common.h"

@interface YZDailyWorkVC () <YZTopScrollViewDelegate, UIScrollViewDelegate, UIGestureRecognizerDelegate>

// 顶部滑动 view
@property (nonatomic, weak) YZTopScrollView *topContianerView;
// 内容滑动 view
@property (nonatomic, weak) UIScrollView *contentScrollView;
// 内容集合
@property (nonatomic, strong) NSArray *contentArray;

@end

float const kTopScrollHeight  = 48;

@implementation YZDailyWorkVC

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.title = @"每日待办";
    // top
    [self setupTopContianerView];
    // content scroll
    [self setupContentScrollView];
    // list vc
    [self setupChildController];
    // view
    [self setupView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // 设置右滑返回的代理
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // 取消右滑返回的代理 使用系统
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

#pragma mark - setup function

- (void)setupTopContianerView {
    
    YZTopScrollView *topContianerView = [[YZTopScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kTopScrollHeight)];
    // 选中 delegate
    topContianerView.delegate = self;
    // 按钮名称
    topContianerView.channelNameArray = @[
                                          ((YZDailyWorkItem*)self.contentArray[0]).name,
                                          ((YZDailyWorkItem*)self.contentArray[1]).name,
                                          ((YZDailyWorkItem*)self.contentArray[2]).name,
                                          ];
    
    
    self.topContianerView = topContianerView;
    [self.view addSubview:topContianerView];
}

- (void)setupContentScrollView {
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    contentScrollView.frame = CGRectMake(0, kTopScrollHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - kRectNavAndStatusHight - kTopScrollHeight);
    contentScrollView.contentSize = CGSizeMake(contentScrollView.frame.size.width * self.contentArray.count, 0);
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.pagingEnabled = YES;
    contentScrollView.delegate = self;
    contentScrollView.bounces = NO;
    contentScrollView.backgroundColor = [UIColor whiteColor];
    
    self.contentScrollView = contentScrollView;
    [self.view insertSubview:contentScrollView atIndex:0];
}

- (void)setupChildController {
    
    for (NSInteger i = 0; i < self.contentArray.count; i++) {
        
        YZDailyWorkListVC *childVC = [[YZDailyWorkListVC alloc] initWithNibName:@"YZDailyWorkListVC" bundle:nil];
        childVC.dailyWorkItem = self.contentArray[i];
        [self addChildViewController:childVC];
    }
    
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
}

- (void)setupView {
    
    // 上角的按钮
    UIBarButtonItem *newWorkButton = [self barButtonItemWithImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue63e", 18, kColorWithHex(0x434343))] highImage:nil target:self action:@selector(newWorkAction)];
    self.navigationItem.rightBarButtonItem = newWorkButton;
    
    
    @weakify(self)
    [RACObserve(self.contentScrollView, contentOffset) subscribeNext:^(NSValue* x) {
        
        @strongify(self)
        // 滑动时 跟随
        CGPoint point = [x CGPointValue];
        [self.topContianerView indicatorViewDidScroll:point.x];
    }];
}

#pragma mark - event delegae

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    //YES：允许右滑返回  NO：禁止右滑返回 当内容页在第一页才支持右滑返回
    return self.contentScrollView.contentOffset.x == 0;
}

#pragma mark - top delegate

// 选择了某个 tab 回调
- (void)chooseChannelWithIndex:(NSInteger)index {
    
    [self.contentScrollView setContentOffset:CGPointMake(self.contentScrollView.frame.size.width * index, 0) animated:YES];
}

#pragma mark - scroll view delegate

// 重新设置了 UIScrollView 的 contentOffset
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (scrollView == self.contentScrollView) {
        
        NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
        YZDailyWorkListVC *vc = self.childViewControllers[index];
        vc.view.frame = self.contentScrollView.bounds;
        vc.view.left = scrollView.contentOffset.x;
        
        if (vc.view.superview == nil) {
            
            [scrollView addSubview:vc.view];
        }
    }
}

// 滑动的减速动画结束后会调用这个方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.contentScrollView) {
        
        // 选中tab
        NSInteger index = scrollView.contentOffset.x / self.contentScrollView.frame.size.width;
        [self.topContianerView selectChannelButtonWithIndex:index];
        // 内容滑动
        [self scrollViewDidEndScrollingAnimation:scrollView];
    }
}

#pragma mark - event actions

- (void)newWorkAction {
    
    // 跳转到新建任务
    [[YZRouter sharedInstance] yz_atuoPush:@"dwNewDailyWork"];
}

#pragma mark - getter

- (NSArray *)contentArray {
    
    if (_contentArray == nil) {
        
        _contentArray = @[
                          [YZDailyWorkItem initItemWithName:@"未完成" withType:DWItemTypeUndo],
                          [YZDailyWorkItem initItemWithName:@"已完成" withType:DWItemTypeDone],
                          [YZDailyWorkItem initItemWithName:@"已忽略" withType:DWItemTypeIgnored]
                          ];
    }
    
    return _contentArray;
}

@end



















